<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="联系电话" prop="contactMobile">
        <el-input
          v-model="queryParams.contactMobile"
          placeholder="请输入联系电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="contactName">
        <el-input
          v-model="queryParams.contactName"
          placeholder="请输入联系人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车牌号" prop="carNum">
        <el-input
          v-model="queryParams.carNum"
          placeholder="请输入车牌号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="办理状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择办理状态" clearable size="small">
          <el-option
            v-for="dict in stateOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="业务来源" prop="bizFrom">
        <el-input
          v-model="queryParams.bizFrom"
          placeholder="请输入业务来源"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="平台流水" prop="outTradeNo">
        <el-input
          v-model="queryParams.outTradeNo"
          placeholder="请输入平台支付流水号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="通道流水" prop="tradeNo">
        <el-input
          v-model="queryParams.tradeNo"
          placeholder="请输入通道支付流水号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="创建时间" prop="gmtTime">
        <el-date-picker
          v-model="dateRange"
          :unlink-panels="unlinkPanels"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['car:transfer:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['car:transfer:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['car:transfer:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="transferList" @selection-change="handleSelectionChange">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-row :gutter="24" class="expand-item">
            <el-col :span="6">车辆所有人：{{props.row.carOwner || '--'}}</el-col>
            <el-col :span="6">品牌型号：{{props.row.brandModel || '--'}}</el-col>
            <el-col :span="6">车辆识别代号：{{props.row.vinNum || '--'}}</el-col>
            <el-col :span="6">发动机号码：{{props.row.engineNum || '--'}}</el-col>
          </el-row>
          <el-row :gutter="24" class="expand-item">
            <el-col :span="6">注册日期：{{props.row.regDate}} </el-col>
            <el-col :span="6">发证日期：{{props.row.issueDate}} </el-col>
            <el-col :span="6">使用性质：{{useTypeFormat(props.row)}} </el-col>
            <el-col :span="6">燃油类型：{{fuelTypeFormat(props.row)}} </el-col>
          </el-row>
          <el-row :gutter="24" class="expand-item">
            <el-col :span="6">人伤事故：{{hasAccidentFormat(props.row)}} </el-col>
            <el-col :span="6">座位数：{{props.row.seat}} </el-col>
            <el-col :span="6">车辆类型：{{props.row.carType}} </el-col>
            <el-col :span="6">车辆性质：{{props.row.useTypeDesp}} </el-col>
          </el-row>

          <el-row :gutter="24" class="expand-item">
            <el-col :span="6">平台流水：{{props.row.outTradeNo}} </el-col>
            <el-col :span="6">通道流水：{{props.row.tradeNo}} </el-col>
            <el-col :span="6">支付渠道：{{props.row.payChannel}} </el-col>
            <el-col :span="6">支付类型：{{props.row.payType}} </el-col>
          </el-row>

          <el-row :gutter="24" class="expand-item">
            <el-col :span="6">业务来源：{{props.row.bizFrom}} </el-col>
            <el-col :span="6">备注：{{props.row.remark}} </el-col>
          </el-row>

        </template>
      </el-table-column>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="联系电话" align="center" prop="contactMobile" />
      <el-table-column label="联系人" align="center" prop="contactName" />
      <el-table-column label="预约时间" align="center" prop="pickTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.pickTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预付定金" align="center" prop="amount" />
      <el-table-column label="办理地区" align="center" prop="province" >
        <template slot-scope="scope">
          <span>{{ scope.row.province }} {{scope.row.city}} {{scope.row.region}}</span>
        </template>
      </el-table-column>
      <el-table-column label="车牌号" align="center" prop="carNum" />
      <el-table-column label="办理状态" align="center" prop="state" :formatter="stateFormat" />
      <el-table-column label="创建时间" align="center" prop="gmtTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gmtTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
        <el-button
          type="text"
          size="mini"
          icon="el-icon-edit"
          @click="handleSl(scope.row.id)"
          v-hasPermi="['car:transfer:sl']"
          v-if="scope.row.state === 4"
        >受理
        </el-button>
        <el-button
          type="text"
          size="mini"
          icon="el-icon-edit"
          @click="handleWj(scope.row.id)"
          v-hasPermi="['car:transfer:wj']"
          v-if="scope.row.state === 8"
        >完结
        </el-button>
        </template>
      </el-table-column>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listTransfer, getTransfer, delTransfer, addTransfer, updateTransfer, exportTransfer,doSl,doWj } from "@/api/member/transfer";

export default {
  name: "Transfer",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 车辆过户表格数据
      transferList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 使用性质  1非营运  2 营运字典
      useTypeOptions: [],
      // 燃油类型  1汽油 2柴油 3新能源字典
      fuelTypeOptions: [],
      // 是否有人伤事故  0 无  1有字典
      hasAccidentOptions: [],
      // 办理状态  1未支付，4待接单，8已接单，12已完成，16已取消，20已退款字典
      stateOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        memberId: undefined,
        contactMobile: undefined,
        contactName: undefined,
        pickTime: undefined,
        provinceCode: undefined,
        cityCode: undefined,
        regionCode: undefined,
        bizFrom: undefined,
        outTradeNo: undefined,
        tradeNo: undefined,
        payChannel: undefined,
        payType: undefined,
      },
      // 表单参数
      form: {},
      dateRange: [],
      // 表单校验
      rules: {
        memberId: [
          { required: true, message: "会员id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("car_use_type").then(response => {
      this.useTypeOptions = response.data;
    });
    this.getDicts("car_fuel_type").then(response => {
      this.fuelTypeOptions = response.data;
    });
    this.getDicts("sys_is_enable").then(response => {
      this.hasAccidentOptions = response.data;
    });
    this.getDicts("car_transfer_state").then(response => {
      this.stateOptions = response.data;
    });
  },
  methods: {
    /** 查询车辆过户列表 */
    getList() {
      this.loading = true;
      listTransfer(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.transferList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleSl(id) {
      this.$confirm('是否确认受理该订单?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return doSl(id);
      }).then(() => {
        this.getList();
        this.msgSuccess("操作成功");
      }).catch(function () {
      });
    },
    handleWj(id) {
      this.$confirm('是否确认完结该订单?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return doWj(id);
      }).then(() => {
        this.getList();
        this.msgSuccess("操作成功");
      }).catch(function () {
      });
    },
    // 使用性质  1非营运  2 营运字典翻译
    useTypeFormat(row, column) {
      return this.selectDictLabel(this.useTypeOptions, row.useType);
    },
    // 燃油类型  1汽油 2柴油 3新能源字典翻译
    fuelTypeFormat(row, column) {
      return this.selectDictLabel(this.fuelTypeOptions, row.fuelType);
    },
    // 是否有人伤事故  0 无  1有字典翻译
    hasAccidentFormat(row, column) {
      return this.selectDictLabel(this.hasAccidentOptions, row.hasAccident);
    },
    // 办理状态  1未支付，4待接单，8已接单，12已完成，16已取消，20已退款字典翻译
    stateFormat(row, column) {
      return this.selectDictLabel(this.stateOptions, row.state);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        memberId: undefined,
        contactMobile: undefined,
        contactName: undefined,
        pickTime: undefined,
        amount: undefined,
        provinceCode: undefined,
        cityCode: undefined,
        regionCode: undefined,
        province: undefined,
        city: undefined,
        region: undefined,
        carNum: undefined,
        carType: undefined,
        carOwner: undefined,
        brandModel: undefined,
        vinNum: undefined,
        engineNum: undefined,
        regDate: undefined,
        issueDate: undefined,
        useType: undefined,
        fuelType: undefined,
        hasAccident: undefined,
        seat: undefined,
        sysCarType: undefined,
        useTypeDesp: undefined,
        state: undefined,
        remark: undefined,
        bizFrom: undefined,
        outTradeNo: undefined,
        tradeNo: undefined,
        payChannel: undefined,
        payType: undefined,
        version: undefined,
        gmtTime: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车辆过户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTransfer(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改车辆过户";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateTransfer(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addTransfer(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除车辆过户编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTransfer(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有车辆过户数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportTransfer(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
